package com.oracle.outsidein;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 按照INSTALL.md安装工具
 * 工具说明: wv=文档转网页; ix=文档转图片; px=文档转PDF
 */
public class OitMain {
    // 工具目录和版本
    static String outsideinHome = "/opt/outsidein";
    static String version = "8.5.5";
    static String platform = "linux";
    static String march = "x86-64";
    // 线程池参数
    static int taskSize = 1; // 线程池中任务数量
    static long taskTimeout = 8000; // 任务超时ms,强制结束
    // 转换参数
    static boolean wv2zip = true; // 是否把网页压缩到zip
    static boolean wv2mht = false; // 是否把网页压缩到mht

    public static void main(String[] args) {
        String src = "/opt/outsidein/tmp/001.docx";
        String dst = "/opt/outsidein/tmp/001.html";
        try {
            // 第一步:设置参数创建实例
            OutsideinManager outsideinManager = OutsideinManager.config()
                    .outsideinHome(outsideinHome)
                    .version(version)
                    .platform(platform)
                    .march(march)
                    .taskSize(taskSize)
                    .taskTimeout(taskTimeout)
                    .build();
            // 第二步:启动实例线程池
            outsideinManager.startup();
            // 第三步:执行任务
            OutsideinConverter
                    .make(outsideinManager)
                    .convert(src)
                    .to(dst)
                    .wv2zip(wv2zip)
                    .execute();
            // 测试停止
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    try {
                        while (true) {
                            outsideinManager.shutdown();
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                Thread.currentThread().interrupt();
                            }
                            if (!outsideinManager.isRun()) {
                                System.exit(0);
                                break;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Thread.currentThread().interrupt();
                    }
                }
            }, 10000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
